package com.otod.sys.code.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.otod.base.service.BaseService;
import com.otod.commons.code.bo.BizCodeBo;
import com.otod.commons.entity.Page;
import com.otod.pm.constants.PmConstant;
import com.otod.sys.code.mapper.CodeDictMapper;
import com.otod.sys.code.mapper.CodeInfoMapper;
import com.otod.sys.code.service.ICodeService;
import com.otod.sys.vo.CodeDictVo;
import com.otod.sys.vo.CodeInfoVo;

/**
 * 码表管理
 * @author jqiu
 */
@Service("codeService")
@Transactional(readOnly=true)
public class CodeServiceImpl extends BaseService implements ICodeService {
	
	@Autowired
	CodeDictMapper codeDictMapper;
	@Autowired
	CodeInfoMapper codeInfoMapper;
	/**
	 * 得到随机数拥有填充主键
	 */
	public String getUUID() {
		return super.getUUID();
	}

	/**
	 * 编码种类查询
	 * @param page
	 * @param param
	 * @return
	 */
	public Page queryCodeDictForList(Page page,Map<String, Object> param){
		List<CodeDictVo> dataList =  codeDictMapper.queryForList(page,param);
		page.setAaData(dataList);
		return page;
	}
	

	/**
	 * 类别删除
	 * @param dictId 类别ID
	 */
	@Transactional
	public void dictId(String dictId){
		codeDictMapper.deleteById(dictId);
	}
	

	/**
	 * 查询码表信息列表
	 * @param dictNo 类别
	 * @return
	 */
	public List<CodeInfoVo> queryCodeInfo(String dictNo){
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("codeDictNo", dictNo);
		return codeInfoMapper.queryForList(param);
	}
	
	/**
	 * 查询码表信息列表
	 * @param dictNo 类别
	 * @return
	 */
	public Page queryCodeInfoPage(Page page,Map<String, Object> param){
		List<CodeInfoVo> list = codeInfoMapper.queryForList(page,param);
		List<CodeInfoVo> codeVoList =new ArrayList<CodeInfoVo>();
		//去除掉项目负责人及项目成员的角色 不能对他们提交项目阶段成果
		for (CodeInfoVo codeInfoVo : list) {
			if(PmConstant.PRJ_ROLE_PRINCIPAL.equals(codeInfoVo.getCodeNo())){
				codeVoList.add(codeInfoVo);
			}
		}
		list.removeAll(codeVoList);
		page.setAaData(list);
		page.setiTotalRecords(list.size());
		return page;
	}
	
	/**
	 * 查询码表信息列表
	 * @return
	 */
	public List<CodeInfoVo> queryCodeInfoAll(){
		Map<String,Object> param = new HashMap<String, Object>();
		return codeInfoMapper.queryForList(param);
	}
	
	
	/**
	 * 查询码表信息BO
	 * @param dictNo 类别
	 * @return
	 */
	public List<BizCodeBo> queryBizCodeBo(String dictNo){
		List<CodeInfoVo> list = this.queryCodeInfo(dictNo);
		List<BizCodeBo> bizList = new ArrayList<>();
		BizCodeBo bo;
		for (CodeInfoVo codeInfoVo : list) {
			bo = new BizCodeBo();
			BeanUtils.copyProperties(codeInfoVo, bo);
			bizList.add(bo);
		}
		return bizList;
	}


	/**
	 * 添加数据  向表 asmg_t_code_dict插入
	 * @param codeNo
	 * @param codeName
	 * 
	 */
	@Transactional
	public void addCodedict(String codeNo, String codeName) {
		//把数据封装到实体类中
		CodeDictVo dict= new CodeDictVo();
		dict.setUuid(super.getUUID());
		dict.setCodeDictNo(codeNo);
		dict.setCodeDictName(codeName);
		// TODO Auto-generated method stub
		codeDictMapper.insert(dict);
	}
	/**
	 * 通过uuid查询 asmg_t_code_info表中的数据
	 * @param uuid
	 * @return
	 */
	public CodeInfoVo getCodeInfoById(String uuid) {
		return codeInfoMapper.getById(uuid);
	}
	/**
	 * 修改表smg_t_code_info详细信息
	 * @return 
	 */
	@Transactional
	public void updateCodeInfo(Map<String,Object> map) {
		// TODO Auto-generated method stub
		 codeInfoMapper.update(map);
	}

	/**
	 * 向表smg_t_code_info插入数据
	 */
	@Transactional
	public void addCodeInfo(CodeInfoVo vo) {
		// TODO Auto-generated method stub
		codeInfoMapper.insert(vo);
	}

	/**
	 * 参数项 删除  表名smg_t_code_info
	 */
	@Transactional
	public void infoId(String infoId) {
		// TODO Auto-generated method stub
		codeInfoMapper.deleteById(infoId);
	}

	@Override
	public CodeInfoVo getCodeInfoByCodeNo(String codeNo) {
		
		return codeInfoMapper.getCodeInfoByCodeNo(codeNo);
	}

	
	/***
	 * 通过 类型查询下拉列表
	 */
	public List<CodeDictVo> queryCodeDict(String codeDictNo) {
		// TODO Auto-generated method stub
		
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("codeDictNo", codeDictNo);
		return codeDictMapper.queryForList(param);
	}

	
	
}
